package com.uui.util;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.uui.bean.User;
@Component
public class PasswordHelper {  

	@Value("${password.algorithmName}")  
	private String algorithmName;  
	@Value("${password.hashIterations}")  
	private int hashIterations;  
    private RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();  
  
  
    public User encryptPassword(User user) {  
  
        user.setSalt(randomNumberGenerator.nextBytes().toHex());  
        System.out.println(algorithmName+"======================"+hashIterations);
        String newPassword = new SimpleHash(  
        		algorithmName,           //加密算法  
                user.getPassword(),      //密码  
                ByteSource.Util.bytes(user.getCredentialsSalt()),  //salt盐   username + salt  
                hashIterations   //迭代次数  
                ).toHex();  
  
        user.setPassword(newPassword);  
        
        return user;
    }  
}  
